package com.yupi.oj.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.oj.common.ErrorCode;
import com.yupi.oj.constant.CommonConstant;
import com.yupi.oj.constant.VoucherConstant;
import com.yupi.oj.exception.BusinessException;
import com.yupi.oj.exception.ThrowUtils;
import com.yupi.oj.mapper.VoucherMapper;
import com.yupi.oj.model.dto.voucher.VoucherQueryRequest;
import com.yupi.oj.model.entity.Voucher;
import com.yupi.oj.service.VoucherService;
import com.yupi.oj.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher> implements VoucherService {

    /**
     * 校验优惠价是否合法
     * @param voucher
     * @param add
     */
    @Override
    public void validVoucher(Voucher voucher, boolean add) {
        if (voucher==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String title = voucher.getTitle();
        String rules = voucher.getRules();
        Long actualValue = voucher.getActualValue();
        Integer type = voucher.getType();
        Integer status = voucher.getStatus();
        Integer stock = voucher.getStock();

        if (add){
            ThrowUtils.throwIf(!ObjectUtil.isAllNotEmpty(title, type, rules, actualValue, status, stock),ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isNotBlank(title) && title.length() > 80){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题过长");
        }
        if (StringUtils.isNotBlank(rules) && rules.length() > 4096){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "规则过长");
        }
        if (ObjectUtil.isNotEmpty(actualValue) && actualValue<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"价值小于零");
        }
        if (ObjectUtil.isNotEmpty(stock) && stock<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"库存小于零");
        }
        if (ObjectUtil.isNotEmpty(type) && !ArrayUtil.contains(VoucherConstant.type,type)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"未选择正确的类型（0折扣卡;1代金券;2会员卡）");
        }
        if (ObjectUtil.isNotEmpty(status) && !ArrayUtil.contains(VoucherConstant.status,status)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"未选择正确的状态（1上架;2下架;3过期）");
        }

    }

    @Override
    public QueryWrapper<Voucher> getQueryWrapper(VoucherQueryRequest voucherQueryRequest) {
        QueryWrapper<Voucher> queryWrapper = new QueryWrapper<>();
        if (voucherQueryRequest==null) {
            return queryWrapper;
        }
        Long id = voucherQueryRequest.getId();
        String title = voucherQueryRequest.getTitle();
        Integer type = voucherQueryRequest.getType();
        Integer status = voucherQueryRequest.getStatus();
        Integer stock = voucherQueryRequest.getStock();
        String sortField = voucherQueryRequest.getSortField();
        String sortOrder = voucherQueryRequest.getSortOrder();
        queryWrapper.like(StringUtils.isNotBlank(title),"title",title);
        queryWrapper.like(ObjectUtil.isNotEmpty(type),"type",type);
        queryWrapper.like(ObjectUtil.isNotEmpty(stock),"stock",stock);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),sortOrder.equals(CommonConstant.SORT_ORDER_ASC),sortField);
        return queryWrapper;
    }
}
